package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.CodeA4Dao;
import com.int_tech.MessageCodeAll.entity.CodeA4;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * Created by liubochao on 2018/6/22.
 */
@Component
public class CheckA4TimeTask {

    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private CodeA4Dao codeA4Dao;

    @Scheduled(cron="0/10 * * * * ?")
    public void checkA4() throws ParseException {

        List<SocketLogSend> socketLogSendList = socketLogSendDao.findByCodeAndCheckStatus("A4",0);

        for(SocketLogSend socketLogSend : socketLogSendList)
        {
            try
            {
                socketLogSend.setCheckStatus(1);
                socketLogSendDao.save(socketLogSend);

                String logData = socketLogSend.getDataString();

                String[] logs = logData.split(",");

                CodeA4 codeA4 = new CodeA4();

                codeA4.setCarNumber(logs[2]);
                codeA4.setDataString(socketLogSend.getDataString());
                codeA4.setLng(logs[7]);
                codeA4.setLat(logs[8]);
                codeA4.setCreatedTime(socketLogSend.getCreateTime());
                codeA4.setStatus(0);

                codeA4Dao.save(codeA4);
            }
            catch (Exception e){

                e.printStackTrace();

                socketLogSend.setCheckStatus(2);

                socketLogSendDao.save(socketLogSend);
            }
        }
    }
}
